package gui.statistics;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import javax.swing.DefaultListModel;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;

import logger.ApplicationLogger;

import org.apache.commons.io.FileUtils;

import conf.PropReaderWriter;

public class StatisticWrapper {

	private  String name;
	private String description;
	private String type;
	private ArrayList<String[]> otherFields;
	private File dataFile = null;

	public StatisticWrapper(File datasheet){
		dataFile= datasheet;
		loadData();
	}

	private void loadData() {
		try{
			WorkbookSettings ws = new WorkbookSettings();
			ws.setLocale(new Locale("en", "EN"));

			Workbook workbook = Workbook.getWorkbook( dataFile,ws);

			Sheet s = workbook.getSheet(0);

			String[] position = PropReaderWriter.getXlsProperty("name_position").split(",");
			int row = Integer.parseInt(position[1]);
			int column = Integer.parseInt(position[0]);
			Cell dc = s.getCell(row,column);
			this.setName(dc.getContents());
			
			//TIPO
			position = PropReaderWriter.getXlsProperty("type_position").split(",");
			row = Integer.parseInt(position[1]);
			column = Integer.parseInt(position[0]);
			dc = s.getCell(row,column);
			this.setType(dc.getContents());
			

			//DESC
			position = PropReaderWriter.getXlsProperty("desc_position").split(",");
			row = Integer.parseInt(position[1]);
			column = Integer.parseInt(position[0]);
			dc = s.getCell(row,column);
			this.setDescription(dc.getContents());
			workbook.close();
			

		}
		catch (IOException e1)
		{
			ApplicationLogger.getLogger().log(java.util.logging.Level.SEVERE, e1.getMessage());
			
		}
		catch (BiffException e)
		{
			ApplicationLogger.getLogger().log(java.util.logging.Level.SEVERE, e.getMessage());
		
		}



	}
	
	public String toString(){
		return dataFile.getAbsolutePath();
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

	public void addOtherField( String nom, String value){
		if(otherFields== null){
			otherFields = new ArrayList<String[]>();
		}

		String[] newField = new String[2];
		newField[0]= nom;
		newField[1]= value;
		otherFields.add(newField);


	}

	public ArrayList<String[]> getOtherFields(){
		return otherFields;
	}
	
	public File getDataFile(){
		return dataFile;
	}



}
